package com.match.competition_apply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.match.competition_apply.entity.CompetitionInfo;
import com.match.competition_apply.entity.vo.CompetitionInfoQuery;
import com.match.competition_apply.mapper.CompetitionInfoMapper;
import com.match.competition_apply.service.CompetitionInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-23
 */
@Service
public class CompetitionInfoServiceImpl extends ServiceImpl<CompetitionInfoMapper, CompetitionInfo> implements CompetitionInfoService {

    @Override
    public Map<String, Object> findCompetitionByQueryPage(long current, long limit, CompetitionInfoQuery competitionInfoQuery) {
        //封装Page对象
        Page<CompetitionInfo> competitionInfoPage=new Page<>(current,limit);
        //封装查询条件
        QueryWrapper<CompetitionInfo> competitionInfoQueryWrapper=new QueryWrapper<>();
        String competitionName = competitionInfoQuery.getCompetitionName();
        String competitionOrganizer = competitionInfoQuery.getCompetitionOrganizer();
        String entryLever = competitionInfoQuery.getEntryLever();
        String competitionType = competitionInfoQuery.getCompetitionType();
        String competitionLever = competitionInfoQuery.getCompetitionLever();
        String roleString  = competitionInfoQuery.getRoles().toString();
        //如果用户角色中不含有系统管理员就查询该用户对应竞赛信息
        if (!roleString.contains("系统管理员")){
            competitionInfoQueryWrapper.eq("teacher_number",competitionInfoQuery.getTeacherNumber());
        }
        if (StringUtils.isEmpty(competitionInfoQuery.getStates()) && roleString.contains("系统管理员")){
            competitionInfoQueryWrapper.notIn("states","0");
        }
        if (!StringUtils.isEmpty(competitionInfoQuery.getStates())){
            competitionInfoQueryWrapper.notIn("states",competitionInfoQuery.getStates());
        }
        if (!StringUtils.isEmpty(competitionName)){
            competitionInfoQueryWrapper.like("competition_name",competitionName);
        }
        if (!StringUtils.isEmpty(competitionOrganizer)){
            competitionInfoQueryWrapper.eq("competition_organizer",competitionOrganizer);
        }
        if (!StringUtils.isEmpty(entryLever)){
            competitionInfoQueryWrapper.eq("entry_lever",entryLever);
        }
        if (!StringUtils.isEmpty(competitionType)){
            competitionInfoQueryWrapper.eq("competition_type",competitionType);
        }
        if (!StringUtils.isEmpty(competitionLever)){
            competitionInfoQueryWrapper.eq("competition_lever",competitionLever);
        }
        competitionInfoQueryWrapper.orderByAsc("gmt_create");
        baseMapper.selectPage(competitionInfoPage,competitionInfoQueryWrapper);
        Map<String,Object> map=new HashMap<>();
        long total = competitionInfoPage.getTotal();
        List<CompetitionInfo> records = competitionInfoPage.getRecords();
        map.put("total",total);
        map.put("records",records);
        return map;
    }

    /**
     * 学生-条件查询所有竞赛信息
     * @param current
     * @param limit
     * @param competitionInfoQuery
     * @return
     */
    @Override
    public Map<String, Object> getAllComByQueryPage(long current, long limit, CompetitionInfoQuery competitionInfoQuery) {
        //封装Page对象
        Page<CompetitionInfo> competitionInfoPage=new Page<>(current,limit);
        //封装查询条件
        QueryWrapper<CompetitionInfo> competitionInfoQueryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(competitionInfoQuery.getTeacherNumber())){
            competitionInfoQueryWrapper.eq("teacher_number",competitionInfoQuery.getTeacherNumber());
        }
        if (!StringUtils.isEmpty(competitionInfoQuery.getCompetitionDepartment())){
            competitionInfoQueryWrapper.eq("competition_department",competitionInfoQuery.getCompetitionDepartment());
        }
        competitionInfoQueryWrapper.eq("states",1);
        competitionInfoQueryWrapper.orderByAsc("gmt_create");
        baseMapper.selectPage(competitionInfoPage,competitionInfoQueryWrapper);
        Map<String,Object> map=new HashMap<>();
        long total = competitionInfoPage.getTotal();
        List<CompetitionInfo> records = competitionInfoPage.getRecords();
        map.put("total",total);
        map.put("records",records);
        return map;
    }


    /**
     * 根据教师职工号得到教师姓名
     * @param teacherNumber
     * @return
     */
    @Override
    public String getTeacherNameByNumber(String teacherNumber) {
        String teacherName=baseMapper.getTeacherName(teacherNumber);
        return teacherName;
    }

}
